import axios from "axios";
import toast from "../toast";

const http = axios.create({
  baseURL: "http://card.api.yinshenme.com/api/card-h5",
  headers: {
    "Content-Type": "application/json",
  },
});

http.interceptors.response.use(async (response) => {
  if (response.data.code != 0) {
    console.error(response.data);
    return Promise.reject(response.data);
  }
  return Promise.resolve(response.data.data);
});

const httpRsp = axios.create({
  baseURL: "http://card.api.yinshenme.com/api/card-h5",
  headers: {
    "Content-Type": "application/json",
  },
});

httpRsp.interceptors.response.use(async (response) => {
  const code = parseInt(response.data.rspCode);
  if (code != 0) {
    console.error(response.data);
    return Promise.reject({
      code,
      message: response.data.rspDesc,
    });
  }
  return Promise.resolve(response.data);
});

/**
 * @typedef Localtion
 * @property {number} phoneyType
 * @property {string} provinceName
 * @property {string} provinceCode
 * @property {string} cityName
 * @property {string} cityCode
 */

/**
 * @typedef Area
 * @property {string} text
 * @property {string} value
 * @property {Area[]} [children]
 */

/**
 * @typedef PhoneNumber
 * @property {string} number
 * @property {string} price
 */

/**
 * @typedef AreaFormData
 * @property {string} code
 * @property {string} name
 */

/**
 * @typedef SubmitFormData
 * @property {{
 *  prov: AreaFormData,
 *  city: AreaFormData,
 *  number: string,
 *  numberKey: string
 * }} numInfo
 * @property {{channel: string}} params
 * @property {{
 *  prov: AreaFormData,
 *  city: AreaFormData,
 *  district: AreaFormData,
 *  address: string,
 *  certName: string,
 *  certNo: string,
 *  receiverMobile: string,
 *  receiverName: string
 * }} postInfo
 */

/**
 * 获取当前ip归属地
 * @param {string} channel
 * @returns {Promise<Localtion>}
 */
export async function getLocaltion(channel) {
  try {
    return await httpRsp.post("/ipconfig", {
      channel,
    });
  } catch (error) {
    toast("获取ip归属地失败: " + error.message);
    return {};
  }
}
/**
 * 回去归属地列表
 * @returns {Promise<Area[]>}
 */
export async function getAreaList() {
  try {
    return await http.post("/phoneList");
  } catch (error) {
    toast("获取地区列表失败: " + error.message);
    return [];
  }
}
/**
 * 回去联通区域列表
 * @returns {Promise<Area[]>}
 */
export async function getLTPostList() {
  try {
    return await http.post("/ltPostList");
  } catch (error) {
    toast("获取地区列表失败: " + error.message);
    return [];
  }
}

/**
 * 选号
 *
 * @param {{
 *  phoneCityCode: string,
 *  phoneCityName: string,
 *  phoneProvinceCode: string,
 *  phoneProvinceName: string,
 *  channel : string,
 *  searchParam?: string,
 *  searchValue?: string
 * }} options
 *
 * @returns {Promise<{
 *  dataList: PhoneNumber[],
 *  splitLen: string,
 *  uuid: string
 * }>}
 */
export async function selectPhone(options) {
  try {
    return await httpRsp.post("/numSelect.htm", {
      areaInfo: {
        phoneCityCode: options.phoneCityCode,
        phoneCityName: options.phoneCityName,
        phoneProvinceCode: options.phoneProvinceCode,
        phoneProvinceName: options.phoneProvinceName,
      },
      params: {
        channel: options.channel,
      },
      searchParam: options.searchParam ? options.searchParam : "",
      searchValue: options.searchValue ? options.searchValue : "",
    });
  } catch (error) {
    toast("选号失败: " + error.message);
    return { dataList: [] };
  }
}

/**
 *
 * @param {SubmitFormData} data
 * @returns {Promies<{
 *  orderNo: string,
 *  preNumber: string,
 *  respJsonsBody: string,
 *  respJsonsResult: string,
 * }>}
 */
export async function submit(data) {
  try {
    const result = await httpRsp.post("/submit", data);
    toast("提交成功", () => {
      location.reload();
    });
    return result;
  } catch (error) {
    toast("提交失败: " + error.message);
  }
}
